package com.huawei.user.service;

import com.alibaba.fastjson.JSONObject;
import com.huawei.user.VO.UserVO;
import com.huawei.user.entry.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class SendMessageService {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    public String sendQueryUserMessage(String phone) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("phone",phone);
        map.put("createTime",createTime);
        //将消息携带绑定键值：UserDirectRouting 发送到交换机SelectUserExchange
        rabbitTemplate.convertAndSend("SelectUserExchange", "SelectUserRouting", map);
        return "ok";
    }


    public String sendUpdateUserMessage(UserVO userVO) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("user", JSONObject.toJSONString(userVO));
        map.put("createTime",createTime);
        //将消息携带绑定键值：UserDirectRouting 发送到交换机UserDirectExchange
        rabbitTemplate.convertAndSend("UpdateUserExchange", "UpdateUserRouting", map);
        return "ok";
    }


}
